/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.softcomputo.composer;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.VariableResolver;

/**
 *
 * @author l2
 * @param <T>
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public abstract class ControladorComposer<T extends Component> extends SelectorComposer<T> {

    public static Logger log = Logger.getLogger(ControladorComposer.class);

    @Override
    public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo) {
        ComponentInfo componentInfo = super.doBeforeCompose(page, parent, compInfo);
        beforeInit();
        return componentInfo;
    }

    @Override
    public void doAfterCompose(T comp) throws Exception {
        super.doAfterCompose(comp);
        params((Map<String, Object>) Executions.getCurrent().getArg());
        init();
        _despuesIniciar();
    }

    public void params(Map<String, Object> map) {
    }

    public void beforeInit() {
    }

    public void _despuesIniciar() {
    }

    public abstract void init() throws Exception;

    public <T> T getParametroSesion(String parametro) {
        return (T) ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getSession().getAttribute(parametro);
    }

    public <T> T getParametroSesionAs(String parametro, Class<T> cl) {
        return (T) ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getSession().getAttribute(parametro);
    }

    public void eliminarParametroSesion(String parametro) {
        ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getSession().removeAttribute(parametro);
    }

    public void setParametroSesion(String parametro, Object dato) {
        ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getSession().setAttribute(parametro, dato);
    }

}
